﻿using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Data;
using System;

namespace TravianLib {
    public partial class bdReqs
    {
        //public List<building> curBuildings_ { get; set; }
       

        //public bool canBuildCrop(building.buildingType GID, int level)
        //{
        //    List<building> curBuildings = (from c in curBuildings_
        //                    where c.status_ == building.status.built
        //                    select c).ToList();
        //    int cropProd = (from c in curBuildings
        //                                    join o in this.costs
        //                                    on
        //                                    new { KEY1 = c.level_, KEY2 = c.gid_ }
        //                                    equals
        //                                    new { KEY1 = o.Level, KEY2 = (building.buildingType)o.GID }
        //                                    where c.gid_ == building.buildingType.Cropland
        //                                    select o.Benefit).Sum() - (from c in curBuildings
        //                                                               join o in this.costs
        //                                                               on
        //                                                               new { KEY2 = c.gid_ }
        //                                                               equals
        //                                                               new { KEY2 = (building.buildingType)o.GID }
        //                                                               where o.Level <= c.level_ && c.status_ == building.status.built
        //                                                               select o.Consumption).Sum() - 2;
        //    try
        //    {
        //        if (cropProd >= getBuildCosts(GID, level).Consumption)
        //        {
        //            return true;
        //        }
        //        else
        //        {
        //            return false;
        //        }
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        public void getBuildTimes(HTTPFetch htp)
        {
            for (int j = 1; j <= 5; j++)
            {
                htp.createHTTPrequest("http://help.travian.com/index.php?type=faq&gid=1", "gameSpeed=1&gameMB=" + j + "&gamesettings.x=52&gamesettings.y=10&gamesettings=send");
                for (int i = 1; i < 41; i++)
                {
                    string output = htp.createHTTPrequest("http://help.travian.com/index.php?type=faq&gid=" + i);
                    DataSet dTmp = ConvertHTMLTablesToDataSet(output);
                    foreach (DataTable dTable in dTmp.Tables)
                    {
                        if (dTable.Rows[0][1].ToString().StartsWith("MB"))
                        {
                            foreach (DataRow dRow in dTable.Rows)
                            {
                                if (!dRow[0].ToString().StartsWith("lvl"))
                                {
                                    for (int o = 1; o < 6; o++)
                                    {
                                        int test = 0;
                                        if (int.TryParse(dRow[0].ToString(), out test))
                                        {
                                            TimeSpan result = new TimeSpan();
                                            string[] arr;
                                            arr = dRow[o].ToString().Split(":".ToCharArray());
                                            if (arr[0] != "-")
                                            {
                                                result = new TimeSpan(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2]));

                                                bdReqs.buildingTimesRow newRow = this.buildingTimes.NewbuildingTimesRow();
                                                newRow.GID = (ushort)i;
                                                newRow.Level = ushort.Parse(dRow[0].ToString());
                                                newRow.MBLevel = ushort.Parse(dTable.Rows[0][o].ToString().Substring(dTable.Rows[0][o].ToString().Length - 2));
                                                newRow.Time = result;
                                                this.buildingTimes.AddbuildingTimesRow(newRow);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                this.buildingTimes.AcceptChanges();
                this.AcceptChanges();
                this.WriteXml("requirements_.xml", System.Data.XmlWriteMode.WriteSchema);
            }
        }
        private static DataSet ConvertHTMLTablesToDataSet(string HTML)
        {
            DataTable dt;
            DataSet ds = new DataSet();
            dt = new DataTable();
            string TableExpression = "<table[^>]*>(.*?)</table>";
            string HeaderExpression = "<th[^>]*>(.*?)</th>";
            string RowExpression = "<tr[^>]*>(.*?)</tr>";
            string ColumnExpression = "<td[^>]*>(.*?)</td>";
            bool HeadersExist = false;
            int iCurrentColumn = 0;
            int iCurrentRow = 0;

            MatchCollection Tables = Regex.Matches(HTML,
            TableExpression,
            RegexOptions.Singleline |
            RegexOptions.Multiline |
            RegexOptions.IgnoreCase);


            foreach (Match Table in Tables)
            {
                iCurrentRow = 0;
                HeadersExist = false;
                dt = new DataTable();

                if (Table.Value.Contains("<th"))
                {
                    HeadersExist = true;

                    MatchCollection Headers = Regex.Matches(Table.Value,
                    HeaderExpression,
                    RegexOptions.Singleline |
                    RegexOptions.Multiline |
                    RegexOptions.IgnoreCase);

                    foreach (Match Header in Headers)
                    {
                        dt.Columns.Add(Header.Groups[1].ToString());
                    }

                }
                else
                {

                    int myvar2222 = Regex.Matches(
                    Regex.Matches(
                    Regex.Matches(
                    Table.Value,
                    TableExpression,
                    RegexOptions.Singleline
                    | RegexOptions.Multiline |
                    RegexOptions.IgnoreCase)[0].ToString(),
                    RowExpression, RegexOptions.Singleline |
                    RegexOptions.Multiline |
                    RegexOptions.IgnoreCase)[0].ToString(),
                    ColumnExpression,
                    RegexOptions.Singleline |
                    RegexOptions.Multiline |
                    RegexOptions.IgnoreCase).Count;

                    for (int iColumns = 1; iColumns <= myvar2222; iColumns++)
                    {
                        dt.Columns.Add("Column " + System.Convert.ToString(iColumns));
                    }

                }

                MatchCollection Rows = Regex.Matches(Table.Value,
                RowExpression,
                RegexOptions.Singleline |
                RegexOptions.Multiline | RegexOptions.IgnoreCase);
                try
                {
                    foreach (Match Row in Rows)
                    {

                        if (!((iCurrentRow == 0) & HeadersExist))
                        {
                            DataRow dr = dt.NewRow();
                            iCurrentColumn = 0;

                            MatchCollection Columns = Regex.Matches(Row.Value,
                            ColumnExpression,
                            RegexOptions.Singleline |
                            RegexOptions.Multiline |
                            RegexOptions.IgnoreCase);

                            foreach (Match Column in Columns)
                            {

                                dr[iCurrentColumn] = Column.Groups[1].ToString();

                                iCurrentColumn++;
                            }

                            dt.Rows.Add(dr);
                        }
                        iCurrentRow++;
                    }
                    ds.Tables.Add(dt);
                }
                catch
                {

                }

            }

            return ds;
        } 
        //public void fillFromOldDataset(database tmp)
        //{
        //    System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex("[0-9 ]+");
        //    this.requirements.Clear();
        //    this.requirements.AddrequirementsRow(1, -1, -1);
        //    this.requirements.AddrequirementsRow(2,-1,-1);
        //    this.requirements.AddrequirementsRow(3, -1, -1);
        //    this.requirements.AddrequirementsRow(4, -1, -1);

        //    this.requirements.AddrequirementsRow(5, 1, 10);
        //    this.requirements.AddrequirementsRow(5,15, 5);

        //    this.requirements.AddrequirementsRow(6, 2, 10);
        //    this.requirements.AddrequirementsRow(6, 15, 5);

        //    this.requirements.AddrequirementsRow(7, 3, 10);
        //    this.requirements.AddrequirementsRow(7, 15, 5);

        //    this.requirements.AddrequirementsRow(8, 4, 5);

        //    this.requirements.AddrequirementsRow(9, 4, 10);
        //    this.requirements.AddrequirementsRow(9, 15, 5);
        //    this.requirements.AddrequirementsRow(9, 8, 5);

        //    this.requirements.AddrequirementsRow(10, 15, 1);

        //    this.requirements.AddrequirementsRow(11, 15, 1);

        //    this.requirements.AddrequirementsRow(12, 15, 3);
        //    this.requirements.AddrequirementsRow(12, 22, 3);

        //    this.requirements.AddrequirementsRow(13, 15, 3);
        //    this.requirements.AddrequirementsRow(13, 22, 1);

        //    this.requirements.AddrequirementsRow(14, 16, 15);

        //    this.requirements.AddrequirementsRow(15, -1, -1);

        //    this.requirements.AddrequirementsRow(16, -1, -1);

        //    this.requirements.AddrequirementsRow(17, 15, 3);
        //    this.requirements.AddrequirementsRow(17, 10, 1);
        //    this.requirements.AddrequirementsRow(17, 11, 1);

        //    this.requirements.AddrequirementsRow(18, 15, 1);

        //    this.requirements.AddrequirementsRow(19, 15, 3);
        //    this.requirements.AddrequirementsRow(19, 16, 1);

        //    this.requirements.AddrequirementsRow(20, 12, 3);
        //    this.requirements.AddrequirementsRow(20, 22, 5);

        //    this.requirements.AddrequirementsRow(21, 10, 5);
        //    this.requirements.AddrequirementsRow(21, 5, 5);

        //    this.requirements.AddrequirementsRow(22, 19, 3);
        //    this.requirements.AddrequirementsRow(22, 15, 3);

        //    this.requirements.AddrequirementsRow(23, -1, -1);

        //    this.requirements.AddrequirementsRow(24, 15, 10);
        //    this.requirements.AddrequirementsRow(24, 22, 10);

        //    this.requirements.AddrequirementsRow(25, 15, 5);

        //    this.requirements.AddrequirementsRow(26, 18, 1);
        //    this.requirements.AddrequirementsRow(26, 15, 5);

        //    this.requirements.AddrequirementsRow(27, 15, 10);

        //    this.requirements.AddrequirementsRow(28, 17, 20);
        //    this.requirements.AddrequirementsRow(28, 20, 10);

        //    this.requirements.AddrequirementsRow(29, 19, 20);

        //    this.requirements.AddrequirementsRow(30, 20, 20);

        //    this.requirements.AddrequirementsRow(31, -1, -1);

        //    this.requirements.AddrequirementsRow(32, -1, -1);

        //    this.requirements.AddrequirementsRow(33, -1, -1);

        //    this.requirements.AddrequirementsRow(34, 15, 5);
        //    this.requirements.AddrequirementsRow(34, 26, 3);

        //    this.requirements.AddrequirementsRow(35, 4, 10);
        //    this.requirements.AddrequirementsRow(35, 3, 99);

        //    this.requirements.AddrequirementsRow(36, 16, 1);

        //    this.requirements.AddrequirementsRow(37, 15, 3);
        //    this.requirements.AddrequirementsRow(37, 16, 1);

        //    this.requirements.AddrequirementsRow(38, 15, 10);
        //    this.requirements.AddrequirementsRow(38, 40, 0);

        //    this.requirements.AddrequirementsRow(39, 15, 10);
        //    this.requirements.AddrequirementsRow(39, 40, 0);

        //    this.requirements.AcceptChanges();
        //    this.AcceptChanges();

        //    this.WriteXml("requirements_.xml");

        //    //this.requirements.AddrequirementsRow(40, 16, 1);


        //    //foreach (System.Data.DataTable tbl in tmp._requirements.Tables)
        //    //{
        //    //    if (tbl.TableName.StartsWith("res"))
        //    //    {
        //    //        foreach (System.Data.DataRow row in tbl.Rows)
        //    //        {
        //    //            bdReqs.costsRow newRow = this.costs.NewcostsRow();
        //    //            int i = 0;
        //    //            if (int.TryParse(row[0].ToString(),out i)) {
        //    //                newRow.GID = int.Parse(rgx.Matches(tbl.TableName)[0].Value);
        //    //                newRow.Level = int.Parse(row[0].ToString().Replace(" ", ""));
        //    //                newRow.Wood = int.Parse(row[1].ToString().Replace(" ", ""));
        //    //                newRow.Clay = int.Parse(row[2].ToString().Replace(" ", ""));
        //    //                newRow.Iron = int.Parse(row[3].ToString().Replace(" ", ""));
        //    //                newRow.Crop = int.Parse(row[4].ToString().Replace(" ", ""));
        //    //                newRow.Consumption = int.Parse(row[5].ToString().Replace(" ", ""));
        //    //                newRow.CP = int.Parse(row[6].ToString().Replace(" ", ""));
        //    //                try
        //    //                {
        //    //                    newRow.Benefit = int.Parse(row[7].ToString().Replace(" ", "").Replace("%", ""));
        //    //                }
        //    //                catch
        //    //                {
        //    //                    newRow.Benefit = -1;
        //    //                }
        //    //                this.costs.Rows.Add(newRow);
        //    //            }
        //    //        }
        //    //    }
        //    //}
        //    //this.costs.AcceptChanges();
        //    //this.AcceptChanges();
        //    //this.WriteXml("requirements_.xml",System.Data.XmlWriteMode.WriteSchema);
        //}
        //private List<double> getProdValues(List<building> curBuildings)
        //{
        //    List<double> output = new List<double>();
        //    output.Add((from c in curBuildings
        //                join o in this.costs on
        //                new { KEY1 = c.gid_, KEY2 = c.level_ }
        //                equals
        //                new { KEY1 = (building.buildingType)o.GID, KEY2 = o.Level }
        //                where c.gid_ == building.buildingType.Woodcutter && c.status_ == building.status.built
        //                select o.Benefit).Sum());
        //    output.Add((from c in curBuildings
        //                join o in this.costs on
        //                new { KEY1 = c.gid_, KEY2 = c.level_ }
        //                equals
        //                new { KEY1 = (building.buildingType)o.GID, KEY2 = o.Level }
        //                where c.gid_ == building.buildingType.ClayPit && c.status_ == building.status.built
        //                select o.Benefit).Sum());
        //    output.Add((from c in curBuildings
        //                join o in this.costs on
        //                new { KEY1 = c.gid_, KEY2 = c.level_ }
        //                equals
        //                new { KEY1 = (building.buildingType)o.GID, KEY2 = o.Level }
        //                where c.gid_ == building.buildingType.IronMine && c.status_ == building.status.built
        //                select o.Benefit).Sum());
        //    output.Add((from c in curBuildings
        //                join o in this.costs
        //                on
        //                new { KEY1 = c.level_, KEY2 = c.gid_ }
        //                equals
        //                new { KEY1 = o.Level, KEY2 = (building.buildingType)o.GID }
        //                where c.gid_ == building.buildingType.Cropland
        //                select o.Benefit).Sum() - (from c in curBuildings
        //                                           join o in this.costs
        //                                           on
        //                                           new { KEY2 = c.gid_ }
        //                                           equals
        //                                           new { KEY2 = (building.buildingType)o.GID }
        //                                           where o.Level <= c.level_ && c.status_ == building.status.built
        //                                           select o.Consumption).Sum());
        //    return output;
        //}
        //public double whenCanBuild(List<building> curBd, building building, List<double> resources)
        //{
        //    double output = 0;
        //    List<double> prodvalues = getProdValues(curBd);
        //    var reqs = (from c in this.costs
        //                where c.GID == (int)building.gid_ && c.Level == building.level_+1
        //                select c).SingleOrDefault();
        //    if (reqs.Wood > resources[0])
        //    {
        //        output = (reqs.Wood - resources[0]) / prodvalues[0];
        //    }
        //    if (reqs.Clay > resources[1])
        //    {
        //        if ((reqs.Clay - resources[1]) / prodvalues[1] > output)
        //        {
        //            output = (reqs.Clay - resources[1]) / prodvalues[1];
        //        }
        //    }
        //    if (reqs.Iron > resources[2])
        //    {
        //        if ((reqs.Iron - resources[2]) / prodvalues[2] > output)
        //        {
        //            output = (reqs.Iron - resources[2]) / prodvalues[2];
        //        }
        //    }
        //    if (reqs.Crop > resources[3])
        //    {
        //        if ((reqs.Crop - resources[3]) / prodvalues[3] > output)
        //        {
        //            output = (reqs.Crop - resources[3]) / prodvalues[3];
        //        }
        //    }
        //    return output;
        //}
        //public bdReqs(bool asdf)
        //{
        //    if (System.IO.File.Exists("requirements_.xml"))
        //    {
        //        this.ReadXml("requirements_.xml");
        //    }
        //}
        //~bdReqs()
        //{
        //    this.WriteXml("requirements_.xml");
        //}
    }
}
